package pl.rzarajczyk.breaktime.gui;

import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pl.rzarajczyk.breaktime.Lang;
import pl.rzarajczyk.breaktime.Localization;
import pl.rzarajczyk.breaktime.utils.Scheduler;
import pl.rzarajczyk.breaktime.utils.SchedulerEvent;
import pl.rzarajczyk.breaktime.BreakTimeSettings;
import pl.rzarajczyk.breaktime.utils.ExtendedJFrame;
import pl.rzarajczyk.breaktime.utils.Utils;

/**
 *
 * @author rzarajczyk
 */
public class BreakWindow extends ExtendedJFrame {

    private int length;

    private BreakTimeSettings settings;
    private Scheduler scheduler;

    private Log log = LogFactory.getLog(getClass());

    private Localization localization;

    public BreakWindow() {
        initComponents();
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        if ( ! gd.isFullScreenSupported() )
            log.warn("Exclusive full screen is not supported by this system");
    }

    public void setSettings(BreakTimeSettings settings) {
        this.settings = settings;
    }

    public void setScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    public void setLocalization(Localization localization) {
        this.localization = localization;
    }

    

    public void open() {
        this.getContentPane().setBackground(Color.BLACK);

        scheduler.schedule(new SchedulerEvent() {
            
            private boolean inProgress = false;
            private int schedule = 0;

            @Override
            public void onTick() {
                if ( inProgress ) {
                    schedule -= Utils.SECOND;
                    BreakWindow.this.tick(schedule);
                }
            }

            @Override
            public void onStart() {
                inProgress = true;
                schedule = settings.getMicroBreakDuration();
                BreakWindow.this.start(schedule);
            }

            @Override
            public void onFinish() {
                inProgress = false;
                BreakWindow.this.stop();
            }

            @Override
            public String toString() {
                return "BreakWindow listener";
            }

            @Override
            public int interval() {
                return settings.getMicroBreakInterval();
            }

            @Override
            public int duration() {
                return settings.getMicroBreakDuration();
            }
        });
    }

    public void start(int length) {
        this.progress.setMaximum(length);
        this.progressLabel.setText("00:00");
        this.progress.setValue(0);
        this.length = length;
        this.setControllsVisible( settings.isShowControlls() );
        this.setFullScreen(true);
        this.setVisible(true);
        log.debug("BreakWindow started");
    }

    public void tick(int current) {
        this.progress.setValue(length - current);
        this.progressLabel.setText( Utils.toTimeString(current) );
    }

    public void stop() {
        this.setVisible(false);
        this.setFullScreen(false);
        log.debug("BreakWindow hidden");
    }

    public void setControllsVisible(boolean show) {
        this.controllsPanel.setVisible(show);
    }

    public boolean isControllsVisible() {
        return this.controllsPanel.isVisible();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        controllsPanel = new javax.swing.JPanel();
        progress = new javax.swing.JProgressBar();
        progressLabel = new javax.swing.JLabel();
        titleLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Break time!");
        setUndecorated(true);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        controllsPanel.setBackground(new java.awt.Color(0, 0, 0));

        progressLabel.setForeground(new java.awt.Color(255, 255, 255));
        progressLabel.setText("jLabel2");

        titleLabel.setFont(new java.awt.Font("Dialog", 1, 24));
        titleLabel.setForeground(new java.awt.Color(255, 255, 255));
        titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        titleLabel.setText("Break time!");

        javax.swing.GroupLayout controllsPanelLayout = new javax.swing.GroupLayout(controllsPanel);
        controllsPanel.setLayout(controllsPanelLayout);
        controllsPanelLayout.setHorizontalGroup(
            controllsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(controllsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(controllsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controllsPanelLayout.createSequentialGroup()
                        .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(progressLabel))
                    .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE))
                .addContainerGap())
        );
        controllsPanelLayout.setVerticalGroup(
            controllsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controllsPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(controllsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(progressLabel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(controllsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(133, Short.MAX_VALUE)
                .addComponent(controllsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(93, 93, 93))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        localization.localize(this);
    }//GEN-LAST:event_formComponentShown

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel controllsPanel;
    private javax.swing.JProgressBar progress;
    private javax.swing.JLabel progressLabel;
    @Lang
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables


}
